<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>使用jsencrypt执行OpenSSL的RSA加密，解密</title>
</head>
<!--引入jsencrypt.js-->
<script src="./jsencrypt.js"></script>
<script type="text/javascript">
    //公钥
    let PUBLIC_KEY = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAj9vjjWA5dplKC5SSLnQq4FxjeZF8zGFEFsTFBx5cVsGvLDzE+vSEQj/OqEsL//14v+eRfv7aCgS6evuvIqaoadaDlBEe33B/4Ygl40qpJI1aCJF594Fy17mmV9+1L6YAWXwiuEdhTIa8i8xADR47zHt/rsLiGEykW6f0ykAYQeClinRFAGbtiDODM+3wGzete/tqXVVuQHpfNeX04+pX/RN7CRjWmPJvek0d2vgGeZ+AlwoNzwcnf1IgeoloYwVqokiuD3IkPwia0O87rXsGHLdllu8dfb9BZeCHQ/YBykRc8Irhv4e/xudK/4j3n+6zOTiLYy+sG/+rbIhe5rykZwIDAQAB';
    //私钥
    let PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCP2+ONYDl2mUoLlJIudCrgXGN5kXzMYUQWxMUHHlxWwa8sPMT69IRCP86oSwv//Xi/55F+/toKBLp6+68ipqhp1oOUER7fcH/hiCXjSqkkjVoIkXn3gXLXuaZX37UvpgBZfCK4R2FMhryLzEANHjvMe3+uwuIYTKRbp/TKQBhB4KWKdEUAZu2IM4Mz7fAbN617+2pdVW5Ael815fTj6lf9E3sJGNaY8m96TR3a+AZ5n4CXCg3PByd/UiB6iWhjBWqiSK4PciQ/CJrQ7zutewYct2WW7x19v0Fl4IdD9gHKRFzwiuG/h7/G50r/iPef7rM5OItjL6wb/6tsiF7mvKRnAgMBAAECggEARO19rRqCzfGKidvo4rskZBXAT9pGqoKcJjCnms6QPb1VmmOPmhKYYEnEUdyLZl9jgMeKbgfcHtq/Ivf+pSKnJPb0WniNi0+S5lPem3YFbw8PKXXSs6Vhij1Yl1g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z+IGMVilnoelC";



    //使用公钥加密
    let encrypt = new JSEncrypt();
    //encrypt.setPrivateKey('-----BEGIN RSA PRIVATE KEY-----'+PRIVATE_KEY+'-----END RSA PRIVATE KEY-----');
    encrypt.setPublicKey('-----BEGIN PUBLIC KEY-----' + PUBLIC_KEY + '-----END PUBLIC KEY-----');
    let encrypted = encrypt.encrypt('ceshi01');
    console.log('加密后数据:%o', encrypted);




    //使用私钥解密
    let decrypt = new JSEncrypt();
    //decrypt.setPublicKey('-----BEGIN PUBLIC KEY-----' + PUBLIC_KEY + '-----END PUBLIC KEY-----');
    decrypt.setPrivateKey('-----BEGIN RSA PRIVATE KEY-----' + PRIVATE_KEY + '-----END RSA PRIVATE KEY-----');
    let uncrypted = decrypt.decrypt(encrypted);
    console.log('解密后数据:%o', uncrypted);
</script>

</html>